package com.findyou.controller;

import com.findyou.pojo.Category;
import com.findyou.pojo.Result;
import com.findyou.service.CategoryService;
import com.findyou.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Title: CategoryController
 * @Author 贾金博
 * @Package com.findyou.controller
 * @Date 2024/3/9 17:17
 * @description:
 */
@RestController
@RequestMapping("category")
@CrossOrigin(origins = "http://localhost:5173")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
       Map mp =  ThreadLocalUtil.get();
       int id = (int) mp.get("id");
       Result result = categoryService.add(id, category);
       return result;
    }

    @GetMapping
    public Result ArticleCategory() {
        //获取user_id
        Map<String, Object> map = ThreadLocalUtil.get(); // 这样参数里面的注解 就可以不用写了
        int id = (int) map.get("id");
        Result result = categoryService.ArticleCategory(id);
        System.out.println("result = " + result);
        return result;
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category ) {
        Result result = categoryService.update(category);
        System.out.println(result);
        return result;
    }

    @GetMapping("detail")
    public Result detail(int id) {
        Result result = categoryService.detail(id);
        return result;
    }

    @DeleteMapping
    public Result delete(int id) {
        Result result = categoryService.delete(id);
        return result;
    }
}
